/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. All rights reserved.
 */

package com.device.idea.demo.view.widget;

import static com.device.idea.demo.constant.Constant.ILLEGAL_INDEX;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.device.idea.demo.R;
import com.device.idea.demo.constant.Constant;
import com.device.idea.demo.core.PenChildMenu;
import com.device.idea.demo.core.PenPickerAdapter;
import com.device.idea.demo.util.LogUtil;
import com.device.idea.demo.util.MethodUtil;
import com.device.idea.whiteboard.model.WhiteBoardPen;

import java.util.ArrayList;
import java.util.List;

/**
 * PenPickerPopWindow
 * 笔类型选择弹出框
 *
 * @since 2020-12-21
 */
public class PenPickerPopWindow extends BaseToolbarPopupWindow {
    static final String TAG = PenPickerPopWindow.class.getSimpleName();

    /**
     * 颜色选择器子数据
     */
    private List<PenChildMenu> mPenChildMenuList;

    private OnPenPickerListener mListener;

    private RecyclerView mRecyclerView;

    private PenPickerAdapter mAdapter;

    /**
     * 构造函数
     *
     * @param context 白板背景色标识
     */
    public PenPickerPopWindow(@NonNull Context context) {
        super(context);
        LogUtil.error(TAG, "context ====" + (mContext == null));
    }

    /**
     * 构造方法
     *
     * @param context 上下文对象
     * @param width popupWindow的宽
     * @param height popupWindow的高
     * @param isTouchable true-触摸popupWindow外部会消失
     */
    public PenPickerPopWindow(@NonNull Context context, int width, int height, boolean isTouchable) {
        super(context, width, height, isTouchable);
    }

    @NonNull
    @Override
    protected View onCreateContentView(@NonNull Context context) {
        View popWindowView = LayoutInflater.from(context).inflate(R.layout.pop_pen_picker, null);

        mRecyclerView = popWindowView.findViewById(R.id.wb_tool_pen_popwindow_recyclerview);
        return popWindowView;
    }

    @Override
    protected String getTag() {
        return TAG;
    }

    /**
     * 选择笔类型监听
     *
     * @since 2020-01-01
     */
    public interface OnPenPickerListener {
        /**
         * 选择笔类型方法
         */
        void onPickerPenType(int color, int penType);
    }

    /**
     * setPenChildMenuList
     *
     * @param listener setPenChildMenuList
     */
    public void setPenChildMenuList(OnPenPickerListener listener) {
        mListener = listener;
    }

    /**
     * updateData
     *
     * @param list WhiteBoardPen list
     * @param currentType pen type
     */
    public void updateData(List<WhiteBoardPen> list, int currentType) {
        List<PenChildMenu> dataList = new ArrayList<>(list.size());
        for (WhiteBoardPen pen : list) {
            Log.i(TAG, "menu pen color=" + pen.getColor() + " type=" + pen.getType());
            PenChildMenu menu = getPenChildMenu(pen.getColor(), pen.getType().getValue());
            if (menu != null) {
                if (currentType == pen.getType().getValue()) {
                    menu.setIsSelected(true);
                } else {
                    menu.setIsSelected(false);
                }
                dataList.add(menu);
            } else {
                Log.i(TAG, "can not find pen res");
            }
        }

        if (mAdapter == null) {
            mPenChildMenuList = new ArrayList<>();
            mPenChildMenuList.addAll(dataList);
            initPenPickerAdapter();
        } else {
            mPenChildMenuList.clear();
            mPenChildMenuList.addAll(dataList);
            mRecyclerView.setLayoutManager(new GridLayoutManager(mContext, mPenChildMenuList.size()));
        }
        mAdapter.notifyDataSetChanged();
    }

    private void initPenPickerAdapter() {
        mRecyclerView.setLayoutManager(new GridLayoutManager(mContext, mPenChildMenuList.size()));
        mAdapter = new PenPickerAdapter(mContext, mPenChildMenuList, R.layout.item_pop_color_picker_4k);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener((view, itemData, position) -> {
            if (mListener != null) {
                mListener.onPickerPenType(itemData.getPenColor(), itemData.getPenType());
            }
            dismiss();
        });
    }

    private PenChildMenu getPenChildMenu(int color, int type) {
        PenChildMenu menu = null;
        int[] resArray = MethodUtil.getPenResFromType(type);
        if (resArray != null) {
            int index = findColorIndex(color);
            if (index != ILLEGAL_INDEX) {
                menu = new PenChildMenu(resArray[index], false, color, type);
            }
        }
        return menu;
    }

    /**
     * findColorIndex
     *
     * @param color color
     * @return index of color
     */
    public static int findColorIndex(int color) {
        int index = ILLEGAL_INDEX;
        for (int i = 0; i < Constant.PICKER_COLORS.length; i++) {
            if (Constant.PICKER_COLORS[i] == color) {
                index = i;
                break;
            }
        }
        return index;
    }
}